{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "url='https://www.zhihu.com/'\n",
    "# 打开浏览器\n",
    "browser = webdriver.Firefox()\n",
    "# browser\n",
    "browser.get(url)\n",
    "browser.implicitly_wait(0.5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 二维码\n",
    "from selenium.webdriver.common.by import By\n",
    "css_selecter='.ZDI--Qq24 > path:nth-child(1)'\n",
    "write_btn=browser.find_element(by=By.CSS_SELECTOR,value=css_selecter)\n",
    "write_btn.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "css_selecter='button.GlobalWriteV2-topItem:nth-child(3) > svg:nth-child(1) > g:nth-child(1) > circle:nth-child(1)'\n",
    "write_btn=browser.find_element(by=By.CSS_SELECTOR,value=css_selecter)\n",
    "write_btn.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "css_selecter='html.itcauecng body.WhiteBg-body div#root div.App main.App-main div.WriteIndexLayout div.WriteIndexLayout-main.WriteIndex.css-10fqe38 div.css-sdgtgb div.css-i6bazn'\n",
    "write_btn=browser.find_element(by=By.CSS_SELECTOR,value=css_selecter)\n",
    "write_btn.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xpath='/html/body/div[1]/div/main/div/div[2]/div[2]/div[1]'\n",
    "input=browser.find_element(by=By.XPATH,value=xpath)\n",
    "input.click()\n",
    "input.send_keys(\"ArithmeticError\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "url='https://account.aliyun.com/register/qr_register.htm'\n",
    "\n",
    "# 打开浏览器\n",
    "browser = webdriver.Firefox()\n",
    "# browser\n",
    "browser.get(url)\n",
    "browser.implicitly_wait(3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "tags": [
     "软件测试 作业1"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<selenium.webdriver.firefox.webdriver.WebDriver (session=\"e03ccabd-0c51-4594-9ceb-fb99165af20b\")>"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "\n",
    "软件测试 作业1\n",
    "\n",
    "\"\"\"\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "\n",
    "def get(url):\n",
    "    '''\n",
    "    用url打开浏览器\n",
    "    '''\n",
    "    global browser\n",
    "    # 注册驱动\n",
    "    browser = webdriver.Firefox()\n",
    "    # 打开浏览器\n",
    "    browser.get(url)\n",
    "    # 查找等待\n",
    "    browser.implicitly_wait(1)\n",
    "    return browser\n",
    "\n",
    "def by(xpath): \n",
    "    '''\n",
    "    使用xpath获取元素\n",
    "    '''\n",
    "    return browser.find_element(By.XPATH, xpath)\n",
    "\n",
    "\n",
    "# 测试url\n",
    "url = 'https://cloud.tencent.com/register'\n",
    "\n",
    "# 测试用例\n",
    "test_case = [\"1t!#$%&'*+-/@t.qw\",\n",
    "             \"_2=?^_`{|}~@a.qw.t\",\n",
    "             \"\",\n",
    "             \"@qq.com\",\n",
    "             \"tang@\",\n",
    "             \"tangqq.com\",\n",
    "             \"∫∮@qq.com\",\n",
    "             \"\\w@qq.com\",\n",
    "             \".q@qq.com\",\n",
    "             \"@12@qq.com\",\n",
    "             \"quanwei@tang\"]\n",
    "\n",
    "get(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "selenium.webdriver.firefox.webdriver.WebDriver"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(browser)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转到邮箱注册\n",
    "xpath = '/html/body/div[3]/div[1]/div[2]/div/div[1]'\n",
    "by(xpath).click()\n",
    "\n",
    "xpath = '/html/body/div[3]/div[1]/div[2]/div/div[2]/a[2]/i'\n",
    "by(xpath).click()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取输入框\n",
    "xpath='/html/body/div[5]/div[2]/div/div[1]/ul[1]/li[1]/div/div/input'\n",
    "input=by(xpath)\n",
    "\n",
    "xpath='/html/body/div[5]/div[1]/div[2]/div/i'\n",
    "email_icon=by(xpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "selenium.webdriver.firefox.webelement.FirefoxWebElement"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试无效字符\n",
    "# ≈≡≠＝≤≥＜＞≮≯∷±＋－×÷／∫∮∝∞∧∨∑∏∪∩∈∵∴⊥‖∠⌒≌∽√（）【】｛｝ⅠⅡ⊕⊙∥αβγδεζηθΔ\n",
    "wrong_eq_class=[i + '@qq.com' for i in \"@!#$%&'*+-/=?^_`.{|}~\"] \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [],
   "source": [
    "def result():\n",
    "    ret = ''\n",
    "    try:\n",
    "        ret = by(\n",
    "            '/html/body/div[5]/div[2]/div/div[1]/ul[1]/li[1]/div/div/p').text\n",
    "    except:\n",
    "        ret = ''\n",
    "    return '合法输入' if ret == '' else '非法输入'\n",
    "\n",
    "\n",
    "def test(target, test_case):\n",
    "    '''\n",
    "    使用test_case对targer做测试\n",
    "    '''\n",
    "    map = {}\n",
    "    for i in test_case:\n",
    "        target.click()\n",
    "        target.clear()\n",
    "        target.send_keys(i)\n",
    "        email_icon.click()\n",
    "        map[i] = result()\n",
    "    return map\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\"1t!#$%&'*+-/@t.qw\": '合法输入',\n",
       " '_2=?^_`{|}~@a.qw.t': '合法输入',\n",
       " '': '合法输入',\n",
       " '@qq.com': '邮箱格式有误，请修改',\n",
       " 'tang@': '邮箱格式有误，请修改',\n",
       " 'tangqq.com': '邮箱格式有误，请修改',\n",
       " '∫∮@qq.com': '合法输入',\n",
       " '\\\\w@qq.com': '邮箱格式有误，请修改',\n",
       " '.q@qq.com': '邮箱格式有误，请修改',\n",
       " '@12@qq.com': '邮箱格式有误，请修改',\n",
       " 'quanwei@tang': '邮箱格式有误，请修改'}"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test(input,test_case)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭浏览器\n",
    "browser.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2610f16b16b5c4620e15008883dd6e156f92c1646bef607c9684cfa10f7a495f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
